package com.zhongchaowuye.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.zhongchaowuye.pojo.Login;
import com.zhongchaowuye.service.LoginService;

@Controller
@RequestMapping("/login")
public class LoginController {

	@Resource
	private LoginService loginService;
	
	@RequestMapping(value ="/getLogin",method = RequestMethod.POST)
	@ResponseBody
	//根据loginId获取Login对象信息
	public Map<String,Object> getLogin(HttpServletRequest request,Login l){
		Map<String,Object> m=new HashMap<>();
		Map<String,Object> reMap=new HashMap<>();
		try {
			Login login=loginService.getLogin(l); //小写的字母L
			reMap.put("login", login);
			reMap.put("status", "success");
			reMap.put("message", "OK");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
			reMap.put("message", e.getMessage());
		}
		return reMap;
	}

	@RequestMapping(value ="/getLoginList",method = RequestMethod.POST)
	@ResponseBody
	//获取列表
	public Map<String,Object> getLoginList(HttpServletRequest request,Login login){
		Map<String,Object> reMap=new HashMap<>();//定义返回值，其中有一项必须是集合，先按照下面的写法
		try {
			//List<Map<String,Object>> rList=loginService.getLoginList(c); //不带分页（注意实现类中的相应写法）
			
			Page<Map<String,Object>> rList = (Page)loginService.getLoginList(login);
			
			//System.out.println(rList);
			reMap.put("rows", rList);
			reMap.put("total", rList.getTotal());
			reMap.put("pageIndex", login.getPageIndex());
			reMap.put("pageSize", login.getPageSize());
			reMap.put("status", "success");
			reMap.put("message", "OK");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
			reMap.put("message", e.getMessage());
		}
		return reMap;
	}
	
	@RequestMapping(value ="/insertLogin",method = RequestMethod.POST)
	@ResponseBody
	//添加
	public Map<String,Object> insertLogin(HttpServletRequest request,Login login)   
	{
		Map<String,Object> reMap=new HashMap<>();//返回值map
		try 
		{
			int flag=loginService.insertLogin(login);
			//int flag = 0;
			if(flag>0)
			{
				reMap.put("status", "success");
				reMap.put("message", "OK");
			}
			else
			{
				reMap.put("status", "error");
				reMap.put("message", "ERROR");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
			reMap.put("message", e.getMessage());
		}
		return reMap;
	}
	
	@RequestMapping(value ="/updateLogin",method = RequestMethod.POST)
	@ResponseBody
	//修改
	public Map<String,Object> updateLogin(HttpServletRequest request,Login login)   
	{
		Map<String,Object> reMap=new HashMap<>();//返回值map
		try 
		{
			int flag=loginService.updateLogin(login);
			if(flag>0)
			{
				reMap.put("status", "success");
				reMap.put("message", "OK");
			}
			else
			{
				reMap.put("status", "error");
				reMap.put("message", "ERROR");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
			reMap.put("message", e.getMessage());
		}
		return reMap;
	}
	
	@RequestMapping(value ="/delLogin",method = RequestMethod.POST)
	@ResponseBody
	//软删除
	public Map<String,Object> delLogin(HttpServletRequest request,Login login)   
	{
		Map<String,Object> reMap=new HashMap<>();//返回值map
		Map<String,Object> m=new HashMap<>();
		try 
		{
			int flag=loginService.delLogin(login); 
			if(flag>0)
			{
				reMap.put("status", "success");
				reMap.put("message", "OK");
			}
			else
			{
				reMap.put("status", "error");
				reMap.put("message", "ERROR");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
			reMap.put("message", e.getMessage());
		}
		return reMap;
	}

	@RequestMapping(value ="/login",method = RequestMethod.POST)
	@ResponseBody
	//使用用户名与密码进行登录
	public Map<String,Object> login(HttpServletRequest request,Login l){
		Map<String,Object> m_session=new HashMap<>();
		Map<String,Object> reMap=new HashMap<>();
		try {
			Login login=loginService.getLoginByUandP(l); //小写的字母L
			List<String> rolesArray=new ArrayList<>();
			reMap.put("login", login);
			if(login==null){
				reMap.put("status", "error");
				reMap.put("message", "用户名与密码错误！");
				return reMap;
			}else{
				String role_str=login.getRole_str();
				rolesArray=Arrays.asList(role_str.split(",")); //将逗号分隔的角色字符串转换为字符串集合
				
				//在session中保留登录用户信息
				m_session.put("login", login);
				m_session.put("rolesArray", rolesArray);
				HttpSession session=request.getSession(); 
				session.setAttribute("session_attribute",m_session); 
				session.setMaxInactiveInterval(30*60);//以秒为单位
			}
			
			reMap.put("rolesArray", rolesArray);
			reMap.put("status", "success");
			reMap.put("message", "OK");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
			reMap.put("message", e.getMessage());
		}
		return reMap;
	}
	
	@RequestMapping(value ="/getSessionAttribute",method = RequestMethod.POST)
	@ResponseBody
	//使用用户名与密码进行登录
	public Map<String,Object> getSessionAttribute(HttpServletRequest request){
		Map<String,Object> reMap=new HashMap<>(); //返回值
		//从session中获取当前登录用户信息
		Map<String,Object> m_session=(Map<String,Object>)request.getSession().getAttribute("session_attribute");
		if(m_session==null){ //未取到session中的信息，或是session已过期
			reMap.put("status", "error");
			reMap.put("message", "登录信息已过期！");
		}else{
			Login login=(Login)m_session.get("login");
			List<String> rolesArray=(List<String>)m_session.get("rolesArray");
			reMap.put("login", login);
			reMap.put("rolesArray", rolesArray);
			reMap.put("status", "success");
			reMap.put("message", "OK");
		}
		return reMap;
	}
	
	//关闭session
	@RequestMapping(value ="/closeSession",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> closeSession(HttpServletRequest request)
	{
		Map<String,Object> reMap=new HashMap<>();
		HttpSession session=request.getSession();
		if(session!=null){
			session.invalidate();//销毁当前session
			session=null;
			reMap.put("message", "session已经销毁！");
		}else{
			reMap.put("message", "session已经失效！");
		}
		reMap.put("status", "success");
		return reMap;
	}

	@RequestMapping(value ="/changePwd",method = RequestMethod.POST)
	@ResponseBody
	//修改
	public Map<String,Object> changePwd(HttpServletRequest request,Login login)   
	{
		Map<String,Object> reMap=new HashMap<>();//返回值map
		try 
		{
			int flag=loginService.changePwd(login);
			if(flag>0)
			{
				Login newL=loginService.getLogin(login);
				reMap.put("login", newL);
				reMap.put("status", "success");
				reMap.put("message", "OK");
				
				//将session中的login对象更新
				Map<String,Object> m_session=(Map<String,Object>)request.getSession().getAttribute("session_attribute");
				m_session.put("login", newL);
			}
			else
			{
				reMap.put("status", "error");
				reMap.put("message", "ERROR");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			reMap.put("status", "error");
			reMap.put("message", e.getMessage());
		}
		return reMap;
	}
}
